S3 TablesをCDK for Terraformでデプロイしてみた

S3 TablesをCDK for Terraformでデプロイしてみた

re:Invent2024で発表されたS3 TablesをCDK for Terraformでデプロイしてみました。
Clock Icon2024.12.16

製造ビジネステクノロジー部の新澤です。

HashiCorp社より、re:Invent2024で発表されたS3 Tablesなどの新機能についてTerraformが対応したとのリリースがありました!S3 TablesのIaC、これはぜひ試さないと!

https://www.hashicorp.com/blog/terraform-launch-day-support-amazon-s3-tables-eks-hybrid-nodes-and-more

概要

上記リリース内容はTerraformの対応のものでしたが、ということは、当然CDK for Terraformでも対応できているはず!
ということで、CDK for Terraformでのデプロイを試してみました。

やってみた

空のディレクトリでCDK for Terraform(以下、CDKTF)の初期化を行います。

CDKTFが未インストールの場合は、下記を参考にインストールしてください。

https://developer.hashicorp.com/terraform/tutorials/cdktf/cdktf-install

$ cdktf init --template=typescript --providers=aws --local
(node:60332) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Note: By supplying '--local' option you have chosen local storage mode for storing the state of your stack.
This means that your Terraform state file will be stored locally on disk in a file 'terraform.<STACK NAME>.tfstate' in the root of your project.
? Project Name s3tables-sample <== プロジェクト名を入力
? Project Description A simple getting started project for cdktf.
? Do you want to start from an existing Terraform project? no. <= 今回は"no"を選択
? Do you want to send crash reports to the CDKTF team? Refer to
https://developer.hashicorp.com/terraform/cdktf/create-and-deploy/configuration-file#enable-crash-reporting-for-the-cli for more information no. <= 今回は"no"を選択

added 2 packages, and audited 58 packages in 755ms

12 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
⠼

(略)

========================================================================================================
[2024-12-15T16:09:37.328] [INFO] default - Checking whether pre-built provider exists for the following constraints:
  provider: aws
  version : latest
  language: typescript
  cdktf   : 0.20.10

[2024-12-15T16:09:41.190] [INFO] default - Found pre-built provider.
Adding package @cdktf/provider-aws @ 19.47.0
[2024-12-15T16:09:41.194] [INFO] default - Installing package @cdktf/provider-aws @ 19.47.0 using npm.
[2024-12-15T16:09:44.650] [INFO] default - Package installed.

初期化できたので、早速実装してみましょう。

main.tsを以下のように変更します。

なお、ネームスペースとテーブルの名前に使える文字は、英小文字・数値・アンダースコアのみとなっていますので気をつけてください。

main.ts
import type { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
import { S3TablesTableBucket } from "@cdktf/provider-aws/lib/s3tables-table-bucket";
import { S3TablesNamespace } from "@cdktf/provider-aws/lib/s3tables-namespace";
import { S3TablesTable } from "@cdktf/provider-aws/lib/s3tables-table";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    const provider = new AwsProvider(this, "AWS", {
      region: "us-east-1",
    });

    // テーブルバケット
    const s3tableBucket = new S3TablesTableBucket(this, "S3TablesBuclet", {
      name: "sample-table",
    });
    // ネームスペース
    const s3tableNamespace = new S3TablesNamespace(this, "S3TablesNamespace", {
      namespace: "sampledb",
      tableBucketArn: s3tableBucket.arn,
    });
    // テーブル
    const s3TablesTable = new S3TablesTable(this, "S3TablesTable", {
      name: "sample_data",
      format: 'ICEBERG',
      namespace: s3tableNamespace.namespace,
      tableBucketArn: s3tableBucket.arn,
    });
  }
}

const app = new App();
new MyStack(app, "cdktf");
app.synth();

それでは早速デプロイしてみましょう。

$ cdktf deploy
(node:83702) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
cdktf  Initializing the backend...
cdktf  Initializing provider plugins...
       - Reusing previous version of hashicorp/aws from the dependency lock file
cdktf  - Using previously-installed hashicorp/aws v5.81.0
cdktf  Terraform has been successfully initialized!

       You may now begin working with Terraform. Try running "terraform plan" to see
       any changes that are required for your infrastructure. All Terraform commands
       should now work.

       If you ever set or change modules or backend configuration for Terraform,
       rerun this command to reinitialize your working directory. If you forget, other
       commands will detect it and remind you to do so if necessary.
cdktf  Terraform used the selected providers to generate the following execution plan.
       Resource actions are indicated with the following symbols:
         + create

       Terraform will perform the following actions:
cdktf    # aws_s3tables_namespace.S3TablesNamespace (S3TablesNamespace) will be created
         + resource "aws_s3tables_namespace" "S3TablesNamespace" {
             + created_at       = (known after apply)
             + created_by       = (known after apply)
             + namespace        = "sampledb"
             + owner_account_id = (known after apply)
             + table_bucket_arn = (known after apply)
           }

         # aws_s3tables_table.S3TablesTable (S3TablesTable) will be created
         + resource "aws_s3tables_table" "S3TablesTable" {
             + arn                       = (known after apply)
             + created_at                = (known after apply)
             + created_by                = (known after apply)
             + format                    = "ICEBERG"
             + maintenance_configuration = {
                 + iceberg_compaction          = {
                     + settings = {
cdktf  + target_file_size_mb = 64
                       }
                     + status   = "enabled"
                   }
                 + iceberg_snapshot_management = {
                     + settings = {
                         + max_snapshot_age_hours = 24
                         + min_snapshots_to_keep  = 1
                       }
                     + status   = "enabled"
                   }
               }
             + metadata_location         = (known after apply)
             + modified_at               = (known after apply)
             + modified_by               = (known after apply)
             + name                      = "sample_data"
             + namespace                 = "sampledb"
             + owner_account_id          = (known after apply)
             + table_bucket_arn          = (known after apply)
             + type                      = (known after apply)
cdktf  + version_token             = (known after apply)
             + warehouse_location        = (known after apply)
           }

         # aws_s3tables_table_bucket.S3TablesBuclet (S3TablesBuclet) will be created
         + resource "aws_s3tables_table_bucket" "S3TablesBuclet" {
             + arn                       = (known after apply)
             + created_at                = (known after apply)
             + maintenance_configuration = {
                 + iceberg_unreferenced_file_removal = {
                     + settings = {
                         + non_current_days  = 1
                         + unreferenced_days = 30
                       }
                     + status   = "enabled"
                   }
               }
             + name                      = "sample-table"
             + owner_account_id          = (known after apply)
           }

       Plan: 3 to add, 0 to change, 0 to destroy.

cdktf  Do you want to perform these actions?
         Terraform will perform the actions described above.
         Only 'yes' will be accepted to approve.
cdktf  Enter a value: yes
cdktf  aws_s3tables_table_bucket.S3TablesBuclet: Creating...
cdktf  aws_s3tables_table_bucket.S3TablesBuclet: Creation complete after 4s [name=sample-table]
cdktf  aws_s3tables_namespace.S3TablesNamespace: Creating...
cdktf  aws_s3tables_namespace.S3TablesNamespace: Creation complete after 0s
cdktf  aws_s3tables_table.S3TablesTable: Creating...
cdktf  aws_s3tables_table.S3TablesTable: Creation complete after 2s [name=sample_data]
cdktf
       Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

No outputs found.

できました!

マネージドコンソールで確認してみましょう。

deployed-s3-tables-with-cdktf
deployed-s3-tables-with-cdktf-2

テーブルバケット、ネームスペース、テーブル、全てデプロイされました。

なお、マネージドコンソールでは現状では存在しか確認できず、メンテナンスの設定やリソースポリシーの確認は行えないため、AWS CLIなどで確認する必要があります。

例えばテーブルバケットのメンテナンス設定の確認は以下のようにします。

$ aws s3tables get-table-bucket-maintenance-configuration --table-bucket-arn arn:aws:s3tables:us-east-1:123456789012:bucket/sample-table
{
    "tableBucketARN": "arn:aws:s3tables:us-east-1:123456789012:bucket/sample-table",
    "configuration": {
        "icebergUnreferencedFileRemoval": {
            "status": "enabled",
            "settings": {
                "icebergUnreferencedFileRemoval": {
                    "unreferencedDays": 30,
                    "nonCurrentDays": 1
                }
            }
        }
    }
}

設定も問題なくデプロイされているようですね。

最後に

AWSでリリースされた新サービスや新機能がCloudFormationで対応されるまでしばらく期間が開くことは結構多いかと思います。

そういった時は、TerraformのようなAWSのAPIを利用するIaCツールはCloudFormation対応を待たずに対応できるため強みを感じますね。

私のように普段IaCはCDKがメインで、TerrafomのHCLではなく開発言語で書きたい方には、CDKTFを使うことでサクッと試すことができ、そして、サクッと後片付けができます(大事!)

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.